/**
 * @description 切换字母大小写
 * @author 李子一
 */

/**
 * 切换字母大小写,正则表达式
 * @param s str
 */
export function switchLetterCase1(s: string): string {
	let res = ''
	const length = s.length
	if (length === 0) return res

	const reg1 = /[a-z]/
	const reg2 = /[A-Z]/

	for (let i = 0; i < length; i++) {
		const c = s[i]
		if (reg1.test(c)) {
			res += c.toLocaleUpperCase()
		} else if (reg2.test(c)) {
			res += c.toLocaleLowerCase()
		} else {
			res += c
		}
	}

	return res
}

/**
 * 切换字母大小写,ASCII 码
 * @param s str
 */
export function switchLetterCase2(s: string): string {
	let res = ''
	const length = s.length
	if (length === 0) return res

	for (let i = 0; i < length; i++) {
		const c = s[i]
		const code = s.charCodeAt(i)

		//大写字母 ASCII
		if (code >= 65 && code <= 90) {
			res += c.toLowerCase()
			// 小写字母 ASCII
		} else if (code >= 97 && code <= 122) {
			res += c.toUpperCase()
		} else {
			res += s[i]
		}
	}

	return res
}

const s = '100aBc$#)DXy@#$%Z'
console.log(s, switchLetterCase1(s))
console.log(s, switchLetterCase2(s))
